<%@ page contentType="text/html; charset=utf-8" language="java"  errorPage="" %>
<%@ page import="com.netscenery.yang.model.Typedef" %>
<%@page import="com.netscenery.yang.model.BuiltinType"%>
<%@page import="java.util.ArrayList"%>
<%@ page import="operate.OperateType"%>
<%@page import="offer.GetDataSource"%>
<%@page import="com.netscenery.yang.model.Type"%>
<%@page import="offer.SessionObj"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
Typedef typedef = new Typedef();
//获取结点对象
Object itemNode = request.getAttribute("itemNode");
Object opType   = request.getAttribute("opType"); //获取操作对象
if( null != itemNode){
	typedef = (Typedef)itemNode;
}
//Session ModuleStatus control
SessionObj sessionObj = (SessionObj)request.getSession().getAttribute("sessionObj");
String control_disabled = "";
if(!sessionObj.getModuleStatus().toString().equalsIgnoreCase("EDITING")){
 	control_disabled ="disabled='disabled'";
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Typedef</title>
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Cache-Control" content="no-cache" />
    <meta http-equiv="Expires" content="0" />
	<link rel="stylesheet" type="text/css" href="<%=basePath%>css/items.css" />
	<link rel="stylesheet" type="text/css" href="<%=basePath%>/css/subItems.css" />
	<link rel="stylesheet" type="text/css" href="<%=basePath%>/css/Tab.css" />
    <script type="text/javascript"  src="<%=basePath%>jstree-v.pre1.0/_lib/jquery.js"></script>
    <script type="text/javascript"  src="<%=basePath%>js/getDerivedType.js"></script>
    <script type="text/javascript"  src="<%=basePath%>js/StringUtil.js"></script>
</head>
<script type="text/javascript">
 //全局变量，记录tab是否来至Union Type的子页面
 var hasUnionType_tab = false; 
  function noticeLoad(){
   <% if( (null != itemNode) 
        &&  (!opType.toString().equalsIgnoreCase(OperateType.LOAD))){ %>
  <%   		 if(opType.toString().equalsIgnoreCase(OperateType.ADD)){ %>
	   		 	 //新增节点时，重新加载数
	   		 	 parent.reloadTree();
  <%   	} %>
			 $("#opNoticeSpan").html("操作成功！");
   		 	 setTimeout(function(){$("#opNoticeSpan").html("")},3000);
   <% }%>  
   }
   
   function editTypeItem(ItemName){
   		$("#mainContent_tab").css("display","none");
   		$(".navTitle").css("display","block");
   		$("#type_tab").css("display","block");
        $("#type_ico").addClass("liCurrent");
        lockedTypede();
        opensrc(ItemName);
   }
   
   function opensrc(ItemName){
        var nodeId = "<%=request.getAttribute("nodeId") %>";
        nodeId = nodeId + "\\" + ItemName; 
        var url = "<%=basePath%>GetItemContentServlet?nodeLevel="+nodeId; 
        //alert(url);
        document.getElementById("Type_Iframe").src=encodeURI(url);//兼容 ie Firefox
   }
   
   function showTypedef(obj){
   	   if(hasUnionType_tab) return; //锁住父页面的Tab
       $("#type_ico").removeClass("liCurrent");
       $("#mainContent_ico").addClass("liCurrent");
       $("#mainContent_tab").css("display","block");
   	   $(".navTitle").css("display","block");
   	   $("#type_tab").css("display","none");
   }
   
   function showType(obj){
   	   if(hasUnionType_tab) return;
       $("#type_ico").addClass("liCurrent");
       $("#mainContent_ico").removeClass("liCurrent");
       $("#mainContent_tab").css("display","none");
   	   $(".navTitle").css("display","block");
   	   $("#type_tab").css("display","block");
   }
   
   function closeTypeEdit(trigger){
        if(hasUnionType_tab) return;
   		$("#mainContent_tab").css("display","block");
   		$(".navTitle").css("display","none");
   		$("#type_tab").css("display","none");
        $("#type_ico").removeClass("liCurrent");
        $("#mainContent_ico").removeClass("liCurrent");
        
        unLockedTypede();
        //parent.reloadTree();
   }
   
   function lockedTypede(){
   		$("#_baseType").attr("disabled","disabled");
        $("#_derivedType").attr("disabled","disabled");
        $("#_typeEdit").attr("disabled","disabled");
   		$("#FrashDerivedButton").attr("disabled","disabled");
        $("#submitButton").attr("disabled","disabled");
   }
   
   function unLockedTypede(){
   		$("#_baseType").removeAttr("disabled");
        $("#_derivedType").removeAttr("disabled");
        $("#_typeEdit").removeAttr("disabled");
        $("#FrashDerivedButton").removeAttr("disabled");
        $("#submitButton").removeAttr("disabled");
   }
   
   $(document).ready(function(){
        $("#submitButton").click(function(){
	 	     $(":input").css("border-color","");
		     if($("#_name").val().trim().length == 0){
		        $("#_name").css("border-color","red");
		        alert("name 不能为空！");
		        return;
		     }
		     if($("#_baseType").val().trim().length == 0){
		        $("#_baseType").css("border-color","red");
		        alert("Type 不能为空！");
		        return;
		     }
		    /* if($("#_default").val().trim().length == 0){
		        $("#_default").css("border-color","red");
		        alert("Default 不能为空！");
		        return;
		     }*/
		    document.getElementById("TypedefForm").submit(); 
		});
		 bodyLoad();
		 noticeLoad();
 	});
 	function bodyLoad(){
 		if($("#_derivedType").html().trim().length != 0){
	 		$("#DerivedType_DIV").css("display", "block");
			$("#DerivedType_DIV").addClass("itemDiv");
			$("#FrashDerivedButton").css("display", "");
 		}
 	}
 //union.js中使用
 function setDisableColor(){
 	$("#mainContent_ico").addClass("liDisabled");
 	$("#mimInfo_ico").addClass("liDisabled");
 	$("#type_ico").addClass("liDisabled");
 }
 function unSetDisableColor(){
 	$("#mainContent_ico").removeClass("liDisabled");
 	$("#mimInfo_ico").removeClass("liDisabled");
 	$("#type_ico").removeClass("liDisabled");
 }
 function closeUnionTypeTab(){
 	 window.frames["Type_Iframe"].closeUnionTypeEdit();
 }
</script>
<body class="body">
 <div class="navTitle" style="display:none;">
   <ul id="navTabTitle">
     <li id="mainContent_ico" onclick="showTypedef(this)">Typedef Edit</li>
     <li id="type_ico"><span onclick="showType(this)">Type Edit</span><img onclick="closeTypeEdit()" class='TitleLiImg' width='10' height='16' src='<%=basePath %>/images/delete.png' /></li>
   </ul>
   <div style="clear:both;"></div>
 </div>
 <div id="type_tab" style="display:none;padding:0px;margin:0px;max-height:560px;height:560px;">
 <iframe width="100%" id="Type_Iframe" name="Type_Iframe"  height=100% border="0" frameborder="0" src=""  ></iframe>
 </div>
 <div id="mainContent_tab" class="" style="padding:0px;margin:0px;">
 <!-- ****************************************************************************************************** -->
 <form id="TypedefForm" method="post" action="<%=basePath %>TypedefOpreateServlet" class="IndexFormStyle" >
   <input type="hidden"  id="nodeId" name="nodeId" value="<%=request.getAttribute("nodeId") %>"  />
   <div id="TableContent" class="tableContent" >
    	<div class="itemTitle">
	         The Typedef [<%=typedef.getName() %>] information  
	 	</div>
	    <div class="itemDiv">
	       <span class="textSpan">&nbsp;Typedef name:</span>
	       <input class="inputText" id="_name" name="_name" value="<%=typedef.getName()==null?"":typedef.getName()%>" readonly="readonly"  />
	       <span class="notNullSpan">*</span>
	   </div>
	   <div class="itemDiv">
	     <span class="textSpan">&nbsp;Default:</span>
	     <input class="inputText" id="_default" name="_default" value="<%=typedef.getDefaultValue()==null?"":typedef.getDefaultValue()%>"  />
	     <!-- <span class="notNullSpan">*</span> -->
	   </div>
	   <div class="itemDiv">
	     <span class="textSpan">&nbsp;Type:</span>
	     <select style="width:305px;   height:23px;" onchange="getDerivedType(this.value)"   class="inputSelect" id="_baseType" name="_baseType">
	          <% ArrayList<String> baseTypeList =  GetDataSource.GetTypeList(); %>
	          <% if(null ==typedef.getType() || null ==typedef.getType().getBase()){ %>
	          	   <option  value="" ></option>  
	          <% } %>
	          <% for(int i = 0 ; i < baseTypeList.size(); i++ ){ %>
	          <%     if(null !=typedef.getType()&& null !=typedef.getType().getBase()&& baseTypeList.get(i).equalsIgnoreCase(typedef.getType().getBase().getFieldName())){ %>  
	                    <option  value="<%= baseTypeList.get(i)%>" selected="selected"><%=baseTypeList.get(i) %></option>
	          <%     }else{ %> 
	                    <option  value="<%= baseTypeList.get(i)%>" ><%=baseTypeList.get(i) %></option>   
	          <%     }
	             } %>
	     </select>
	     <input type="button" id="_typeEdit" value="Edit" onclick="editTypeItem('<%=typedef.getType()==null?"":typedef.getType().getName()%>')" style="display:<% if(null == typedef.getType() || typedef.getType().getClass() == Type.class){ %>none<%} %>" />
	     <span class="notNullSpan">*</span>
	     <input type="hidden" id="_typehideValue" value="<%=typedef.getType()==null?"":typedef.getType().getName()%>" />
	   </div>
	   <div id="DerivedType_DIV" style="display:none;" >
	     <span class="textSpan">&nbsp;DerivedType:</span>
	     <select style="width:305px;   height:23px;"   class="inputSelect" id="_derivedType" name="_derivedType">
	      <% if(null !=typedef.getType()&& typedef.getType().isDerivedType()){ %>
             <option  value="<%= typedef.getType().getDerived().getName() %>" selected="selected"><%=typedef.getType().getDerived().getName() %></option>
	     <% } %>
	     </select>
	     <input type="button" id="FrashDerivedButton" style="display:none;" value = " Modify " onclick ="FrashDerivedType()" />
	     <span class="notNullSpan">*</span>
	   </div>
	   <div class="itemDiv">
	     <span class="textSpan">&nbsp;Status:</span>
	     <select style="width:305px;   height:23px;"   class="inputSelect" id="_status" name="_status">
	          <% for(int i = 0 ; i < GetDataSource.getStatusList().length ; i++ ){ %>
	          <%     if(typedef.getStatus().getStatus().equalsIgnoreCase(GetDataSource.getStatusList()[i])){ %>  
	                    <option  value="<%= GetDataSource.getStatusList()[i]%>" selected="selected"><%=GetDataSource.getStatusList()[i] %></option>
	          <%     }else{ %> 
	                    <option  value="<%= GetDataSource.getStatusList()[i]%>" ><%=GetDataSource.getStatusList()[i] %></option>   
	          <%     }
	             } %>
	     </select>
	   </div>
	   <div class="itemDiv">
	     <span class="textSpan">&nbsp;Units:</span>
	     <input class="inputText" id="_units" name="_units" value="<%=typedef.getUnits()==null?"":typedef.getUnits()%>"  />
	   </div>
	   <div class="itemDiv">
	   	   <span class="textSpan">&nbsp;Description:</span><br/>
	       <textarea class="textArea"  id="description" name="description"><%=typedef.getDescription()==null?"":typedef.getDescription() %></textarea>
	   </div>
	   <div class="itemDiv">
	   	   <span class="textSpan">&nbsp;Reference:</span><br/>
	       <textarea class="textArea"  id="reference" name="reference"><%=typedef.getReference()==null?"":typedef.getReference() %></textarea>
	   </div>
   
   </div>
   <div id="submitContent" class="submitContent">
     <span id="opNoticeSpan" style="color:red;" ></span>
     <span class="textSpan">&nbsp;</span>
     <input type="button" id="submitButton" <%=control_disabled %>  value="Apply"  />
   </div>
   </form>
 <!-- ****************************************************************************************************** -->
 </div>
</body>
</html>